from gameobjects.vector3 import Vector3

class Plane(object):

  FRONT = 0
  BACK = 1
  BOTH = 2
  AXIAL_X = 0
  AXIAL_Y = 1
  AXIAL_Z = 2

  def __init__(self, normal=None, distance=0, type=None):
    self.normal = normal or Vector3()
    self.distance = distance
    self.type = None

  def distance_to(self, point):
    return self.normal.dot(point) - self.distance

  def side(self, point):
    if self.distance_to(point) >= 0:
      return self.FRONT
    else:
      return self.BACK

  def side_for_box(self, point, mins, maxs):
    if self.type != None:
      if self.distance <= mins[self.type]:
        return self.FRONT
      if self.distance >= maxs[self.type]:
        return self.BACK
      return self.BOTH
    v1, v2 = Vector3()
    for i in range(3):
      if self.normal[i] < 0:
        v1[i] = mins[i]
        v2[i] = maxs[i]
      else:
        v1[i] = maxs[i]
        v2[i] = mins[i]
    d1 = self.normal.dot(v1) - self.distance
    d2 = self.normal.dot(v2) - self.distance
    if d1 >= 0 and d2 >= 0:
      return self.FRONT
    elif d1 < 0 and d2 < 0:
      return self.BACK
    else:
      return self.BOTH

  @classmethod
  def type_for_normal(cls, normal):
    if normal.x == 1:
      return cls.AXIAL_X
    elif normal.y == 1:
      return cls.AXIAL_Y
    elif normal.z == 1:
      return cls.AXIAL_Z
    else:
      return None
